function data2fftModSTHarmonic
format long e;

% Load the data files for the moderate-amplitude experiment.
% The first column is time in seconds.  The data collection rate was 350Hz.
% This data is different than the other sets because it has 8,500 rows.
% Multiply the second column by -0.239 to get surface displacement.
% Note that epsilon is defined by epsilon=2*k0*|a0|.
% Note that the data at each gauge starts at t=0.
% The carrier mode is the 82nd position in the fft.

modx50=load('modx50.out');
modx100=load('modx100.out');
modx150=load('modx150.out');
modx200=load('modx200.out');
modx250=load('modx250.out');
modx300=load('modx300.out');
modx350=load('modx350.out');
modx400=load('modx400.out');
modx450=load('modx450.out');
modx500=load('modx500.out');
modx550=load('modx550.out');
modx600=load('modx600.out');

datax50=[modx50(:,1) -0.239*modx50(:,2)];
datax100=[modx100(:,1) -0.239*modx100(:,2)];
datax150=[modx150(:,1) -0.239*modx150(:,2)];
datax200=[modx200(:,1) -0.239*modx200(:,2)];
datax250=[modx250(:,1) -0.239*modx250(:,2)];
datax300=[modx300(:,1) -0.239*modx300(:,2)];
datax350=[modx350(:,1) -0.239*modx350(:,2)];
datax400=[modx400(:,1) -0.239*modx400(:,2)];
datax450=[modx450(:,1) -0.239*modx450(:,2)];
datax500=[modx500(:,1) -0.239*modx500(:,2)];
datax550=[modx550(:,1) -0.239*modx550(:,2)];
datax600=[modx600(:,1) -0.239*modx600(:,2)];

NNN=length(modx50);

fftx50=fft(datax50(:,2))/NNN;
fftx100=fft(datax100(:,2))/NNN;
fftx150=fft(datax150(:,2))/NNN;
fftx200=fft(datax200(:,2))/NNN;
fftx250=fft(datax250(:,2))/NNN;
fftx300=fft(datax300(:,2))/NNN;
fftx350=fft(datax350(:,2))/NNN;
fftx400=fft(datax400(:,2))/NNN;
fftx450=fft(datax450(:,2))/NNN;
fftx500=fft(datax500(:,2))/NNN;
fftx550=fft(datax550(:,2))/NNN;
fftx600=fft(datax600(:,2))/NNN;

% Write the initial conditions to a file.
% These initial conditions are needed by the Fortran solvers.

ZeroModeLoc=163;
deltak=1/datax50(NNN,1);
kvec=transpose(0:deltak:(NNN-1)*deltak);

k0=1.52951590372984113
epsilon=2*k0*abs(fftx50(ZeroModeLoc))

% Compute M norms and exponential fit.

x50Mnorm=0;
x100Mnorm=0;
x150Mnorm=0;
x200Mnorm=0;
x250Mnorm=0;
x300Mnorm=0;
x350Mnorm=0;
x400Mnorm=0;
x450Mnorm=0;
x500Mnorm=0;
x550Mnorm=0;
x600Mnorm=0;

for j=122:204
    x50Mnorm=x50Mnorm+abs(fftx50(j))^2;
    x100Mnorm=x100Mnorm+abs(fftx100(j))^2;
    x150Mnorm=x150Mnorm+abs(fftx150(j))^2;
    x200Mnorm=x200Mnorm+abs(fftx200(j))^2;
    x250Mnorm=x250Mnorm+abs(fftx250(j))^2;
    x300Mnorm=x300Mnorm+abs(fftx300(j))^2;
    x350Mnorm=x350Mnorm+abs(fftx350(j))^2;
    x400Mnorm=x400Mnorm+abs(fftx400(j))^2;
    x450Mnorm=x450Mnorm+abs(fftx450(j))^2;
    x500Mnorm=x500Mnorm+abs(fftx500(j))^2;
    x550Mnorm=x550Mnorm+abs(fftx550(j))^2;
    x600Mnorm=x600Mnorm+abs(fftx600(j))^2;
end
 
positions=epsilon^2*k0*[0,50,100,150,200,250,300,350,400,450,500,550]';
xdata=epsilon^2*k0*linspace(0,600,100)';
Mnorms=(k0/epsilon)^2*[x50Mnorm,x100Mnorm,x150Mnorm,x200Mnorm,x250Mnorm,x300Mnorm,x350Mnorm,x400Mnorm,x450Mnorm,x500Mnorm,x550Mnorm,x600Mnorm]';
logMnorms=log(Mnorms);
coeffs=polyfit(positions,logMnorms,1);
Mpred=exp(polyval(coeffs,xdata));
delta=-coeffs(1)/2
L2Scaling=exp(2*delta.*(positions));
ScaledMnorms=Mnorms.*L2Scaling;

figure(3)
subplot(2,1,1)
hold off
scatter(positions,Mnorms,'filled','k')
hold on
plot(xdata,Mpred,'r','LineWidth',2)
text(0.05,0.12,strcat('Computed \delta=',num2str(delta)),'FontSize',13,'FontName','Times')
ylabel('$\mathcal{M}$','Interpreter','Latex')
legend('data','best fit exp','Location','NorthEast')
title('$\mathcal{M}$','Interpreter','Latex')

subplot(2,1,2)
hold off
scatter(positions,ScaledMnorms,'filled','k')
hold on
plot([0 0.25],[(k0/epsilon)^2*x50Mnorm (k0/epsilon)^2*x50Mnorm],'r','LineWidth',2)
text(0.05,0.47,strcat('Using \delta=',num2str(delta)),'FontSize',13,'FontName','Times')
xlabel('$\chi$','Interpreter','Latex')
ylabel('$\mathcal{M}$','Interpreter','Latex')
leg1=legend('scaled data','$\mathcal{M}(0)$','Location','NorthEast');
set(leg1,'Interpreter','Latex');
title('Scaled $\mathcal{M}$','Interpreter','Latex')

